package com.bing.jh.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.jh.entity.DzWzLevelConfig;
import com.bing.jh.entity.dto.DzWzLevelConfigDTO;
import com.bing.jh.mapper.DzWzLevelConfigMapper;
import com.bing.jh.service.DzWzLevelConfigService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wangqilong
 * @description 针对表【dz_wz_level_config(成本随销得分等级配置)】的数据库操作Service实现
 * @createDate 2024-06-06 14:20:17
 */
@Service
public class DzWzLevelConfigServiceImpl extends ServiceImpl<DzWzLevelConfigMapper, DzWzLevelConfig>
        implements DzWzLevelConfigService {

    @Override
    public boolean insertOrUpdate(DzWzLevelConfigDTO dzWzLevelConfigDTO) {
        String startTime = dzWzLevelConfigDTO.getStartTime();
        String endTime = dzWzLevelConfigDTO.getEndTime();
        //获取两个月份之间的月份集合
        List<String> months = getMonthsBetweenDates(startTime, endTime);
        List<DzWzLevelConfig> configList = dzWzLevelConfigDTO.getConfigList();
        String username = "1";
        for (String month : months) {
            final String dataMonth = month.replace("-", "");
            configList.forEach(config -> {
                config.setDataMonth(dataMonth);
                config.setUpdateTime(new Date());
                config.setOperator(username);
            });

            List<DzWzLevelConfig> list = baseMapper.selectList(Wrappers.lambdaQuery(DzWzLevelConfig.class)
                    .eq(DzWzLevelConfig::getDataMonth, dataMonth));

            List<DzWzLevelConfig> updateList = new ArrayList<>();
            List<DzWzLevelConfig> saveList = new ArrayList<>();

            for (DzWzLevelConfig config : configList) {
                boolean exists = list.stream().anyMatch(existingConfig ->
                        existingConfig.getDataMonth().equals(config.getDataMonth()) &&
                                existingConfig.getScoreLevel().equals(config.getScoreLevel()) &&
                                existingConfig.getRankPct().equals(config.getRankPct())
                );
                if (exists) {
                    updateList.add(config);
                } else {
                    saveList.add(config);
                }
            }

            if (CollUtil.isNotEmpty(updateList)) {
                updateList.forEach(dzWzLevelConfig -> {
                    baseMapper.updateBatch(dzWzLevelConfig);
                });
            }
            if (CollUtil.isNotEmpty(saveList)) {
                saveBatch(saveList);
            }
        }
        return true;
    }

    /**
     * 获取两个月份之间的月份集合
     *
     * @param startTime
     * @param endTime
     * @return months
     */
    public static List<String> getMonthsBetweenDates(String startTime, String endTime) {
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM");

        LocalDate startDate = LocalDate.parse(startTime + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate endDate = LocalDate.parse(endTime + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        List<String> months = new ArrayList<>();

        while (!startDate.isAfter(endDate)) {
            months.add(startDate.format(outputFormatter));
            startDate = startDate.plus(1, ChronoUnit.MONTHS);
        }

        return months;
    }
}




